package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.metagranth.service.QuestionService;
import com.metagurukul.metagranth.vo.MetaData;

/**
 * Servlet implementation class AdminAnswerServlet
 * This servlet is responsible for functionality of admin page.
 * It will retrieve the QuestionId of a particular question on which admin needs to aprove the Answer.
 * On clicking the question, this Servlet will call the functions responsible for getting metadata List of that particular Question.
 * Which Includes it all posted answers so far, along with the username by whom the Question and Answers was posted.
 * And Date & Time of Posting.
 * @author team MetaGranth
 *
 */
public class AdminAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminAnswerServlet() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * this function will get the question Id of a clicked Question on admin home page.
	 * Will send the Question Id to the service class function getQuestionDetailByQuestionId(questionId)
	 * that will get the metadata of that particular question.
	 * Once the MetaData List is retrieved the entire MetaData is dispatched to the adminquestiondisplay.jsp page
	 * and will display the same on the admin page.
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		if(session.getAttribute("userId")!=null){
			try {
				//get the question Id of the Clicked Question
				int questionId = Integer.parseInt(request.getParameter("questionId"));
				//getQuestionDetailByQuestionId(questionId) gets the metadata of the question.
				MetaData metaData = QuestionService.getQuestionDetailByQuestionId(questionId);
				request.setAttribute("metaData", metaData);
				//dispatches the metaData to the adminquestiondisplay.jsp page and will display the questions there
				request.getRequestDispatcher("adminquestiondisplay.jsp").forward(request, response);
			} catch (Exception exception) {
				
				request.setAttribute("errormsg", exception.getMessage());
				request.getRequestDispatcher("errorPage.jsp").forward(request,response);
			}
		} else {
			response.sendRedirect("index.jsp");
		}
	}
}